package cn.yzc.config;/*
 * Copyright (c) 2023, 2023,yzc.cn All rights reserved.
 *
 */

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

/**
 * <p>Project: SpringMVCPro - DbConfig</p>
 * <p>Powered by yzc On 2023-10-30 15:57:09</p>
 * <p>描述：<p>
 *
 * @author yzc [yzc0415@126.com]
 * @version 1.0
 * @since 17
 */

@Configuration
@PropertySource("classpath:db.properties")
public class DbConfig {
    @Value("${db.driver}")
    String driver;
    @Value("${db.url}")
    String url;
    @Value("${db.username}")
    String username;
    @Value("${db.password}")
    String password;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        return dataSource;
    }

    //配置mybatis设置
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        //设置数据源
        factoryBean.setDataSource(dataSource);


        //设置映射文件的位置
        //运行使用通配符 读取类路径下的资源文件
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = null;
        try {
            resources = resolver.getResources("classpath:cn/yzc/mapper/*Mapper.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //设置别名
        factoryBean.setTypeAliasesPackage("cn.yzc.pojo");

        PageInterceptor interceptor = new PageInterceptor();
        Properties props = new Properties();
        props.setProperty("helperDialect", "mysql"); //使用MySQL数据库时需设置该属性
        props.setProperty("reasonable", "true"); //开启合理化查询，如果开启，pageNum<=0时会查询第一页，pageNum>pages（超过总数时），会查询最后一页
        interceptor.setProperties(props);

        factoryBean.setPlugins(new Interceptor[]{interceptor});


        //mybatis配置
        org.apache.ibatis.session.Configuration cfg =new org.apache.ibatis.session.Configuration();
        //设置mybatis日志输出在控制台
        cfg.setLogImpl(StdOutImpl.class);
        //开启mybatis的驼峰命名规则
        cfg.setMapUnderscoreToCamelCase(true);
        factoryBean.setConfiguration(cfg);


        try {
            return factoryBean.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 事务管理器
     *
     * @param dataSource
     * @return
     */
    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource) {
        DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }


}